﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace EOrm.Helper
{
    /// <summary>
    /// 字符串帮助类
    /// </summary>
    internal class StringHelper
    {
        /// <summary>
        /// 转换字符串为分割符模式
        /// </summary>
        /// <param name="source">原始字符串</param>
        /// <param name="separator">字符串分隔符</param>
        /// <returns>转换后的结果</returns>
        internal static string ToSeparator(string source, char separator = '_')
        {
            if (string.IsNullOrEmpty(source))
            {
                return source;
            }
            List<char> chars = new List<char>();
            for (int i = 0; i < source.Length; i++)
            {
                if (_upperLetter.Contains(source[i]))
                {
                    if (i != 0)
                    {
                        chars.Add(separator);
                    }
                    chars.Add(source[i].ToString().ToLower()[0]);
                }
                else
                {
                    chars.Add(source[i]);
                }
            }
            return new string(chars.ToArray());
        }

        /// <summary>
        /// 大写字母
        /// </summary>
        private static List<char> _upperLetter = new List<char>(){ 'A', 'B', 'C', 'D', 'E', 'F', 'G',
        'H', 'I', 'J', 'K', 'L', 'M', 'N',
        'O', 'P', 'Q', 'R', 'S', 'T', 'U',
        'V', 'W', 'X', 'Y', 'Z'};

        /// <summary>
        /// 正则：判定字符串是否为合法IP地址
        /// </summary>
        /// <param name="origin">原始字符串</param>
        /// <returns>判断结果</returns>
        internal static bool RegexIsIP(string origin)
        {
            //判断是否为IP
            return Regex.IsMatch(origin, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
    }
}
